<?php

class AdvancedSearchWidget extends Widget {

    public $model;
    public $formView;
    public $gridViewId;
    public $note;

    /**
     * Initializes the widgets
     */
    public function init() {
        parent::init();

        if ($this->gridViewId === null)
            $this->gridViewId = 'grid';
        
        $this->note=CHtml::openTag('p', array('class' => 'note'))
                    .Lang::t('advanced.search.lbl.note')
                    .CHtml::closeTag('p');
    }

//end function

    /**
     * Execute the widgets
     */
    public function run() {
        Yii::beginProfile(get_class($this));

        Yii::app()->clientScript->registerScript('search', "
			$('.search-button').click(function(){
			    $('.search-form').toggle();
			    return false;
			});
			$('.search-form form').submit(function(){
			    $.fn.yiiGridView.update('" . $this->gridViewId . "', {
			        data: $(this).serialize()
			    });
			    return false;
			});
		");

        echo CHtml::link(Lang::t('advanced.search.lbl.form.toggle'), '#', array('class' => 'search-button'));
        echo CHtml::openTag('div', array('class' => 'search-form', 'style' => 'display:none'));

        Yii::beginProfile('t2');
        $this->controller->renderPartial($this->formView, array(
            'model' => $this->model,
            'note'=>$this->note,
            'formConfig' => array(
                'action' => Yii::app()->createUrl($this->controller->route),
                'method' => 'get',
            ),
        ));
        Yii::endProfile('t2');

        echo CHtml::closeTag('div');
        echo '<!-- search-form -->';

        Yii::endProfile(get_class($this));
    }//end function
}//end class